<?php
namespace App\Http\Controllers\Admin;
use App\Common\Utill\Utills;
use App\Http\Controllers\Controller;
use App\Models\WebsiteStyle;
use App\Service\WebsitestyleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class WebsitestyleController extends Controller
{
    /**
     * 展示页面
     * @return
     * */
    public function index()
    {
        $adminId = WebsitestyleService::getAdminId();
        return view('admin.websitestyle.index')->with('adminId', $adminId);
    }

    /**
     * 首页数据
     * @param Request $request
     * @return mixed
     */
    public function listData(Request $request)
    {
        $num = $request->input('limit',self::ADMIN_PAGE_NUM);
        $server = new WebsitestyleService();
        $result = $server->getListData($request,$num);
        return Utills::resultData($result);
    }

    /**
     * 添加
     * @return void
     */
    public function createForm()
    {
        return view('admin.websitestyle.create');
    }

    /**
     * 添加保存
     * @param Request $request
     * @return void
     */
    public function store(Request $request){
        $validator = Validator::make($request->all(), [
            'names' => 'required',
            'content' => 'required',
        ], [
            'names.required' => '网站风格不能为空',
            'content.required' => '内容不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $names = $request->input('names',"");
        $content = $request->input('content',"");
        $server = new WebsitestyleService();
        $result = $server->store($names,$content);
        return Utills::resultData($result);
    }

    /**
     * 网站编辑
     */
    public function editForm(Request $request)
    {
        $id = $request->input('id');
        $data = WebsiteStyle::query()->where('id',$id)->first();
        return view('admin.websitestyle.edit')
            ->with('data',$data);
    }

    public function update(Request $request){
        $validator = Validator::make($request->all(), [
            'names' => 'required',
            'content' => 'required',
        ], [
            'names.required' => '网站风格不能为空',
            'content.required' => '内容不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id',"");
        $names = $request->input('names',"");
        $content = $request->input('content',"");
        $server = new WebsitestyleService();
        $result = $server->update($id,$names,$content);
        return Utills::resultData($result);
    }

    /**
     * 状态修改
     * @return void
     */
    public function status(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 404, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id');
        $server = new WebsitestyleService();
        $result = $server->status($id);
        return response()->json($result);
    }

    /*
   * 删除
   */
    public function delete(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error('删除失败');
        }
        $id = $request->input('id');
        $server = new WebsitestyleService();
        $result = $server->delOne($id);
        return Utills::resultData($result);
    }

    /**
     * 展示页面
     * @param Request $request
     * @return
     * */
    public function show(Request $request)
    {
        $id = $request->input('id');
        $data = WebsiteStyle::query()->where('id',$id)->first();
        return view('admin.websitestyle.show')->with('data',$data);
    }

}
